
var SYSTEM_MAIN_URL =  'firechar.com';var client = navigator.userAgent.toLowerCase();var IE = ((client.indexOf("msie") != -1) && (client.indexOf("opera") == -1));var helpleft = 0;var helptop = 0;var openerqr = document.getElementById("messageform");var imageTag = false;var theSelection = false;var clientPC = navigator.userAgent.toLowerCase();var clientVer = parseInt(navigator.appVersion);var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)&&(clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)&&(clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));var is_moz = 0;var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));var is_mac = (clientPC.indexOf("mac")!=-1);bbcode = new Array();
	bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]', '[s]', '[/s]', '[left]', '[/left]', '[center]', '[/center]', '[right]', '[/right]', '[ul]\n [li]\n[/li]\n', '[/ul]', '[ol]\n [li]\n[/li]\n', '[/ol]', '[quote]', '[/quote]', '[code]', '[/code]', '[sup]', '[/sup]', '[sub]', '[/sub]', '[wiki]', '[/wiki]', '[html]', '[/html]', '[h1]', '[/h1]','[h2]','[/h2]','[h3]','[/h3]','[h6]','[/h6]', '[justify]', '[/justify]','[h4]','[/h4]','[h5]','[/h5]');
	imageTag = false;

var draged;

windowses = new Array();

document.onmousemove = mouse_pos;
	var mouse_position = document.createElement('div');
	function help(helptext) {
		document.body.appendChild(mouse_position);
		mouse_position.id = 'help';
		mouse_position.innerHTML = helptext;
	}
	function unhelp() {
		document.body.removeChild(document.getElementById('help'))
	}
	function mouse_pos(e) {
		if (IE) {
			helptop = event.clientY + document.body.scrollTop;
			helpleft = event.clientX + document.body.scrollLeft;
		} else {
				helptop = e.pageY;
				helpleft = e.pageX;
		}
		mouse_position.style.top = helptop + 'px';
		mouse_position.style.left = helpleft + 'px';
		return true;
	}

var time;
	function FitToContent() {
		var text = document.getElementById('messageform');
		if (!text) return;
		if (text.scrollHeight>200) {
			text.style.height = text.scrollHeight + "px";
		}
	}

function addtube(form) {
		var prm = prompt('URL: ');
		if (prm) {
			addtext('[tube='+prm+']',form);
		}
		return false;
	}

function getarraysize(thearray) {for (i = 0; i < thearray.length; i++) {if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))return i;}return thearray.length;}
	function arraypush(thearray,value){thearray[getarraysize(thearray)] = value;}
	function arraypop(thearray) {
		thearraysize = getarraysize(thearray);
		retval = thearray[thearraysize - 1];
		delete thearray[thearraysize - 1];
		return retval;
	}
	function emoticon(text,id) {
		var txtarea = document.getElementById(id);
		text = ' ' + text + ' ';
		if (txtarea.createTextRange && txtarea.caretPos) {
			var caretPos = txtarea.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
			txtarea.focus();
		} else {
			txtarea.value  += text;
			txtarea.focus();
		}
	}
	function emoticon_wospaces(text, id) {
		var txtarea = document.getElementById(id);
		if (txtarea.createTextRange && txtarea.caretPos) {
			var caretPos = txtarea.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
			txtarea.focus();
		} else {
			txtarea.value  += text;
			txtarea.focus();
		}
	}
	function bbstyle(bbnumber,id) {
		var txtarea = document.getElementById(id);
		txtarea.focus();
		donotinsert = false;
		theSelection = false;
		bblast = 0;
		if (bbnumber == -1) {
			while (bbcode[0]) {
				butnumber = arraypop(bbcode) - 1;
				txtarea.value += bbtags[butnumber + 1];
			}
			imageTag = false;
			txtarea.focus();
			return;
		}
		if (is_ie && is_win) {
			theSelection = document.selection.createRange().text; // Get text selection
			if (theSelection) {
				document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
				txtarea.focus();
				theSelection = '';
				return;
			}
		}
		else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
			mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
			return;
		}
		for (i = 0; i < bbcode.length; i++) {
			if (bbcode[i] == bbnumber+1) {
				bblast = i;
				donotinsert = true;
			}
		}
		if (donotinsert) {
			while (bbcode[bblast]) {
					butnumber = arraypop(bbcode) - 1;
					txtarea.value += bbtags[butnumber + 1];
					imageTag = false;
				}
				txtarea.focus();
				return;
		} else {		
			txtarea.value += bbtags[bbnumber];
			arraypush(bbcode,bbnumber+1);
			txtarea.focus();
			return;
		}
		storeCaret(txtarea);
	}
	function bbfontstyle(bbopen, bbclose, id) {
		var txtarea = document.getElementById(id);
		if (is_ie && is_win) {
			theSelection = document.selection.createRange().text;
			if (!theSelection) {
				txtarea.value += bbopen + bbclose;
				txtarea.focus();
				return;
			}
			document.selection.createRange().text = bbopen + theSelection + bbclose;
			txtarea.focus();
			return;
		} else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
			mozWrap(txtarea, bbopen, bbclose);
			return;
		} else {
				txtarea.value += bbopen + bbclose;
				txtarea.focus();
		}
		storeCaret(txtarea);
	}
	function select_or_insert_label(text, id, first) {
		var txtarea = document.getElementById(id);
		if (is_ie && is_win) {
			theSelection = document.selection.createRange().text;
			if (!theSelection) {
				var prm = prompt(text);
				if (prm) {
					txtarea.value += first + prm + ' ';
					txtarea.focus();
					return;
				}
			}
			document.selection.createRange().text = first + theSelection + ' ';
			txtarea.focus();
			return;
		} else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
			mozWrap(txtarea, first, ' ');
			return;
		} else {
				var prm = prompt(text);
				if (prm) {
					txtarea.value += first + prm + ' ';
					txtarea.focus();
					return;
				}
		}
		storeCaret(txtarea);
	}
	function select_or_insert_list(id, first) {
		var txtarea = document.getElementById(id);
		if (is_ie && is_win) {
			theSelection = document.selection.createRange().text;
			if (!theSelection) {
				addtext('\n' + first + ' aaa\n' + first + ' bbb\n' + first + ' ccc\n',id);
				txtarea.focus();
				return;
			}
			var arr_words = theSelection.split('\n');
			theSelection = '';
			for(i = 0; i < arr_words.length; i++) {
				theSelection += first + ' ' + arr_words[i] + '\n';
			}
			document.selection.createRange().text = theSelection;
			txtarea.focus();
			return;
		} else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
			var selLength = txtarea.textLength;
			var selStart = txtarea.selectionStart;
			var selEnd = txtarea.selectionEnd;
			if (selEnd == 1 || selEnd == 2) 
				selEnd = selLength;
			var s1 = (txtarea.value).substring(0,selStart);
			var s2 = (txtarea.value).substring(selStart, selEnd)
			var s3 = (txtarea.value).substring(selEnd, selLength);
			
			var arr_words = s2.split('\n');
			s2 = '';
			for(i = 0; i < arr_words.length; i++) {
				s2 += first + ' ' + arr_words[i] + '\n';
			}			
			txtarea.value = s1 + s2 + s3;
			return;
		} else {
				addtext('\n' + first + ' aaa\n' + first + ' bbb\n' + first + ' ccc\n',id);
				txtarea.focus();
				return;
		}
		storeCaret(txtarea);
	}
	function mozWrap(txtarea, open, close) {
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd == 1 || selEnd == 2) 
			selEnd = selLength;

		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + open + s2 + close + s3;
		return;
	}

	function storeCaret(textEl) {
		if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
	}

	var selection = false;

	function putname(name) { 
		emoticon_wospaces('[b]'+name+'[/b]\n', 'messageform');
		document.getElementById('messageform').focus(); 
		return true; 
	}
	function addtext(name, form) { 
		emoticon_wospaces(name + '\n', form);
		document.getElementById(form).focus(); 
		return true; 
	}

function insert_image_content(form) {
		var wstr = '';
		var hstr = '';
		if (document.getElementById('imgUrl').value.length>1) {
			if (document.getElementById('imgW').value.length>1) {
				wstr = ' width="'+document.getElementById('imgW').value+'"';
			}
			if (document.getElementById('imgH').value.length>1) {
				hstr = ' height="'+document.getElementById('imgH').value+'"';
			}
			if (document.getElementById('imgAlt').value.length>1) {
				altstr = ' alt="'+document.getElementById('imgAlt').value+'"';
			}
			addtext('[img'+altstr+wstr+hstr+']'+document.getElementById('imgUrl').value+'[/img]', form);
			remove_window('_insert_image');
		}
	}
	function insert_link_content(form) {
		var title = '';
		if (document.getElementById('linkUrl').value.length>1) {
			if (document.getElementById('linkTitle').value.length>1) {
				title = document.getElementById('linkTitle').value;
			} else {
					title = document.getElementById('linkUrl').value;
			}
			if ((document.getElementById('linkUrl').value.toLowerCase().indexOf('http') == -1) && (document.getElementById('linkUrl').value.toLowerCase().indexOf('ftp') == -1) && (document.getElementById('linkUrl').value.toLowerCase().indexOf('irc') == -1)) {
				document.getElementById('linkUrl').value = 'http://'+document.getElementById('linkUrl').value;
			}
			addtext('[url='+document.getElementById('linkUrl').value+']'+title+'[/url]', form);
			remove_window('_insert_link');
		}
	}

function remove_window(id) {
		document.body.removeChild(document.getElementById('window'+id));
		delete windowses['window'+id];
		return false;
	}

// Color Picker Script from Flooble.com
	// For more information, visit 
	//	http://www.flooble.com/scripts/colorpicker.php
	// Copyright 2003 Animus Pactum Consulting inc.
	// You may use and distribute this code freely, as long as
	// you keep this copyright notice and the link to flooble.com
	// if you chose to remove them, you must link to the page
	// listed above from every web page where you use the color
	// picker code.
	//---------------------------------------------------------
	// Modifed by FireChar
	var div_id;
	var perline = 9;
    var curId;
    var colorLevels = Array('0', '3', '6', '9', 'C', 'F');
    var colorArray = Array();
    var nocolor = 'none';
    function addColor(r, g, b) {
		var red = colorLevels[r];
     	var green = colorLevels[g];
     	var blue = colorLevels[b];
     	addColorValue(red, green, blue);
    }
    function addColorValue(r, g, b) {
		colorArray[colorArray.length] = '#' + r + r + g + g + b + b;
    }     
    function setColor(color) {
		var link = document.getElementById(curId);
		var field = document.getElementById(curId + 'field');
		var picker = document.getElementById('colorpicker');
		field.value = color;
		if (color == '') {
			link.style.background = nocolor;
			link.style.color = nocolor;
			color = nocolor;
		} else {
				link.style.background = color;
				link.style.color = color;
		}
		picker.style.display = 'none';
		eval(document.getElementById(curId + 'field').title);
    }        
    function Cpicker(div, id) {
		colorArray = Array();
		genColors();
		create_window('<span style="font-family:Verdana; font-size:11px;"><br>'+getColorTable(id)+'<center><a href="http://www.flooble.com/scripts/colorpicker.php" target="_blank">color picker</a> by <a href="http://www.flooble.com" target="_blank"><b>flooble</b></a></center></span>','', 'Цвет','color_picker');
		div_id = div.id;
    }
    function genColors() {
		addColorValue('0','0','0');
        addColorValue('3','3','3');
        addColorValue('6','6','6');
        addColorValue('8','8','8');
        addColorValue('9','9','9');                
        addColorValue('A','A','A');
        addColorValue('C','C','C');
        addColorValue('E','E','E');
        addColorValue('F','F','F');
        for (a = 1; a < colorLevels.length; a++)
			addColor(0,0,a);
        for (a = 1; a < colorLevels.length - 1; a++)
			addColor(a,a,5);

        for (a = 1; a < colorLevels.length; a++)
			addColor(0,a,0);
        for (a = 1; a < colorLevels.length - 1; a++)
			addColor(a,5,a);
			
        for (a = 1; a < colorLevels.length; a++)
			addColor(a,0,0);
        for (a = 1; a < colorLevels.length - 1; a++)
			addColor(5,a,a);			
        for (a = 1; a < colorLevels.length; a++)
			addColor(a,a,0);
        for (a = 1; a < colorLevels.length - 1; a++)
			addColor(5,5,a);
			
        for (a = 1; a < colorLevels.length; a++)
			addColor(0,a,a);
        for (a = 1; a < colorLevels.length - 1; a++)
			addColor(a,5,5);

        for (a = 1; a < colorLevels.length; a++)
			addColor(a,0,a);			
        for (a = 1; a < colorLevels.length - 1; a++)
			addColor(5,a,5);		
       	return colorArray;
     }
    function getColorTable(id) {
		var colors = colorArray;
		var tableCode = '';
		tableCode += '<table border="0" cellspacing="0" cellpadding="0">';
        for (i = 0; i < colors.length; i++) {
			if (i % perline == 0) { tableCode += '<tr>'; }
			tableCode += '<td><a style="color: ' 
            + colors[i] + '; background: ' + colors[i] + ';font-size: 20px;" title="' 
            + colors[i] + '" href="#" onclick="bbfontstyle(\'[color='+colors[i]+']\', \'[/color]\', \''+id+'\');remove_window(\'color_picker\');return false;"">   </a></td>';
            if (i % perline == perline - 1) { tableCode += '</tr>'; }
        }
        if (i % perline != 0) { tableCode += '</tr>'; }
        tableCode += '</table>';
      	return tableCode;
    }

function create_window(inner,margin,title,id) {
		if (windowses['window'+id] == null) {
			var div = document.createElement('div');
			windowses['window'+id] = div;
			div.onclick = function() {
				div.style.zIndex = 99;
				var win;
				for (win in windowses) {
					windowses[win].style.zIndex = 95;
				}
				div.style.zIndex = 99;
			}
			div.onmousedown = function() {
				div.style.zIndex = 99;
				var win;
				for (win in windowses) {
					windowses[win].style.zIndex = 95;
				}
				div.style.zIndex = 99;
			}
			document.body.appendChild(div);
			div.className = 'draged_window';
			div.id = 'window'+id;
			div.innerHTML = '<table border="0"><tr><td><div class="b1"><b></b></div><div class="b2"><b><i><q></q></i></b></div><div class="b3"><b><i></i></b></div><div class="b4"><b></b></div><div class="b5"><b></b></div><div class="window_main"><div id="'+id+'butoons_panel" style="cursor: move;" class="buttons"><table style="width: 100%; height:10px"><tr><td align="left">'+title+'</td><td align="right"><img class="window_close_button" src="http://firechar.com/files/templates/default/images/close1.png" onmouseover="this.src = \'http://firechar.com/files/templates/default/images/close2.png\';" onmouseout="this.src = \'http://firechar.com/files/templates/default/images/close1.png\';" onclick="remove_window(\''+id+'\')"></td></tr></table></div><div class="window_content" style="padding: 7px 0.75em">'+inner+'</div></div><div class="b5b"><b></b></div><div class="b4b"><b></b></div><div class="b3b"><b><i></i></b></div><div class="b2b"><b><i><q></q></i></b></div><div class="b1b"><b></b></div></td></tr></table>';
			make_dragle(div, document.getElementById(id+'butoons_panel'));
		}
	}

function make_dragle(divtodrag, divbydrag) {
		if(!divbydrag) return;
		if(!divtodrag) return;
		document.onmouseup = function() {
			window.clearInterval(draged);
			return false;
		}
		document.getElementById(divbydrag.id).onmousedown = function() {
			drag(document.getElementById(divtodrag.id));
			return false;
		}
	}

function drag(div) {
		var pos = absoliute_pos(div);
		var y = pos['left'];
		var x = pos['top'];
		var mouse_y = helpleft;
		var mouse_x = helptop;
		draged = window.setInterval(function () {div.style.top = (helptop - mouse_x + x) + 'px'; div.style.left = (helpleft - mouse_y + y) + 'px';},24);
	}

function absoliute_pos(div) {
		var ret = new Array();
		var top = div.offsetTop;
		var left = div.offsetLeft;
		while(div.offsetParent) {
			div = div.offsetParent;
			top+= div.offsetTop;
			left+= div.offsetLeft;
		}
		ret['top'] = top;
		ret['left'] = left;
		return ret;
	}

function jsframe(url, element_id) {
		dark();
		var element = document.getElementById(element_id);
		var tmp = element.innerHTML;
		var ret = ajax.s_send(url, null,'get');
		if (ret != false) {
			element.innerHTML = ret;
		} else {
				element.innerHTML = tmp;
		}
		undark();
	}

function AJAX() {
		this.http = false;
		this.init = function() { 
		if (window.XMLHttpRequest) {
			try {
				this.http = new XMLHttpRequest();
			} catch (e){}
		} else if (window.ActiveXObject) {
			try {
				this.http = new ActiveXObject('Msxml2.XMLHTTP');
			} catch (e){
				try {
					this.http = new ActiveXObject('Microsoft.XMLHTTP');
				} catch (e){}
			}
		}
	  }   
	  this.s_send = function (url, data, method) { 
		if (this.http) {
			this.http.open(method, url, false);
			if (method == 'post') {this.http.setRequestHeader('Content-Type',  'application/x-www-form-urlencoded');}
			this.http.send(data);
			if(this.http.status == 200) {
				return this.http.responseText;
			} else {
					return false;
			}
		}
	  }
	}
	var ajax = new AJAX();
	ajax.init();

function undark() {
		document.body.removeChild(document.getElementById('dark'))
	}

function dark() {
		var el = document.createElement('div');
		document.body.appendChild(el);
		el.id = 'dark';
el.innerHTML = '<img src="http://firechar.com/files/templates/default/images/ajax_loading.gif" style="top: 0%; left: 50%; position: fixed;" alt="Загрузка" title="Загрузка">';
}

function insertlink(id) {
		create_window('<div id="insertlinkdivc"></div>','','Вставить ссылку','_insert_link');
		jsframe('http://firechar.com/index.php?morefunct=morefunct&editor_insert_link=1&form='+id, 'insertlinkdivc');
	}

function insertimg(id) {
		create_window('<div id="inserimgdivc"></div>','','Вставить картинку','_insert_image');
		jsframe('http://firechar.com/index.php?morefunct=morefunct&editor_insert_image=1&form='+id, 'inserimgdivc');
	}

